#include "net/packetbuf.h"
#include "lib/random.h"
#include <string.h>

#include "net/mac/framer.h"
#include "framer-nrf24.h"

static int create(void)
{
	framenrf24_t frame;
	uint8_t u8HdrLen = RIMEADDR_CONF_SIZE+RIMEADDR_CONF_SIZE;

	rimeaddr_copy((rimeaddr_t *)&frame.dest_addr, packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
	rimeaddr_copy((rimeaddr_t *)&frame.src_addr, &rimeaddr_node_addr);
	packetbuf_hdralloc(u8HdrLen);
	memcpy(packetbuf_hdrptr(), &frame, packetbuf_hdrlen());

	return u8HdrLen+packetbuf_datalen();	
}

static int parse(void)
{
	framenrf24_t frame;
	uint8_t u8HdrLen = RIMEADDR_CONF_SIZE+RIMEADDR_CONF_SIZE;
	int len;
 	len = packetbuf_datalen();

	rimeaddr_copy((rimeaddr_t *)&frame.dest_addr, packetbuf_dataptr());
	rimeaddr_copy((rimeaddr_t *)&frame.src_addr, packetbuf_dataptr()+RIMEADDR_CONF_SIZE);

	packetbuf_hdrreduce(u8HdrLen);

	if(rimeaddr_cmp((rimeaddr_t *)&frame.dest_addr, &rimeaddr_null))		//broadcast address
	{
		packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (rimeaddr_t *)&frame.dest_addr);
	}

	packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (rimeaddr_t *)&frame.src_addr);

	return len - u8HdrLen;
}

const struct framer framer_nrf24 = {
  create, parse
};
  

